class Reservation < ActiveRecord::Base
    validates_presence_of :name, :pickup_location, :dropoff_location
    validates_numericality_of :guests
    validates_format_of :email, :with => /\A[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|ca|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\z/i
    validates_format_of :phone_number, :with => /^(\d{1}?[-]?)?([(]?\d{3}[) -]?\s?\d{3}-?\d{4})$/, :message => " is invalid"
    VEHICLES = [
        ["Town Car", "Town Car"],
        ["SUV", "SUV"],
        ["Stretch Limo", "Stretch Limo"],
        ["Van", "Van"]
    ]
    def reserve_for_string
        #formatter: strftime("%B %d at %I:%M %p")
        get_time_str(self.reserve_for, Time.now)
    end
    
    def reserve_for_string=(reserve_for_str)
        self.reserve_for = Time.parse(reserve_for_str)
    rescue ArgumentError
        @reserve_for_invalid = true
    end
    
    def pick_up_at_string
        #formatter: strftime("%B %d at %I:%M %p")
        get_time_str(pick_up_at)
    end
    
    def pick_up_at_string=(pick_up_str)
        unless pick_up_str.empty?
            self.pick_up_at = Time.parse(pick_up_str)
        end
    rescue ArgumentError
        @pick_up_at_invalid = true
    end
    
    def get_time_str(property, value = "")
      if value != ""
        return Time.now.strftime("%B %d, %I:%M %p") if property.nil?
      else
        return ""
      end
      return property.strftime("%B %d, %I:%M %p")
    end
           
    def validate
        errors.add(:reserve_for, "is invalid") if @reserve_for_invalid
        errors.add(:pick_up_at, "is invalid") if @pick_up_at_invalid
    end
end